package com.action.Parameter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.entity.Parameter;
import com.entity.Substation;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.service.ParameterService;

public class ParameterAction extends ActionSupport implements SessionAware {

	private ParameterService parameterService;
	private Map session;
	private JSON indexSearchJSON;
	private String deviceType;
	private Parameter parameter;
	private String deviceModel;

	
	public String getDeviceModel() {
		return deviceModel;
	}

	public void setDeviceModel(String deviceModel) {
		this.deviceModel = deviceModel;
	}

	public Parameter getParameter() {
		return parameter;
	}

	public void setParameter(Parameter parameter) {
		this.parameter = parameter;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public JSON getIndexSearchJSON() {
		return indexSearchJSON;
	}

	public void setIndexSearchJSON(JSON indexSearchJSON) {
		this.indexSearchJSON = indexSearchJSON;
	}

	public String getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}

	public String findDeviceModel() throws IOException {
		List<Parameter> Parameterlist = new ArrayList<Parameter>();
		Parameterlist = parameterService.findAllDeviceModelByDeviceType(deviceType);
		JSONArray jsonArray = JSONArray.fromObject(Parameterlist);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(jsonArray);
		return null;
	}
	
	public String initAddParameter(){
		List<Parameter> Parameterlist=new ArrayList<Parameter>();
		Parameterlist = parameterService.findAllDeviceType();
		session.put("deviceTypeList" , Parameterlist);
		return SUCCESS;
	}
	
	public String addParameterMethod(){
		parameterService.addParameter(parameter);
		return SUCCESS;
	}
	
	public String findParameterByDeviceTypeAndDeviceModel(){
		Parameter parameter = parameterService.findParameterByDeviceTypeAndDeviceModel(deviceType, deviceModel);
		session.put("ParameterValue" , parameter);
		return SUCCESS;
	}
	
}
